using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

using EditDeploymentSettings.Properties;

namespace EditDeploymentSettings
{
	class DeploymentEnvironments
	{
		private List<DeploymentEnvironment> _environmentList = new List<DeploymentEnvironment>();
		private List<string> _mergedPropertyNames = new List<string>();

		public DeploymentEnvironments()
		{
		}

		public bool Load(string folder)
		{
			_environmentList.Clear();
			_mergedPropertyNames.Clear();

			string filePattern = "*_settings.xml";
			if (string.IsNullOrEmpty(Settings.Default.FilePattern))
			{
				filePattern = Settings.Default.FilePattern;
			}

			string[] files = Directory.GetFiles(folder, filePattern);
			foreach (string file in files)
			{
				DeploymentEnvironment environment = new DeploymentEnvironment(file);
				_environmentList.Add(environment);


				foreach (string propertyName in environment.Properties.Keys)
				{
					if (!_mergedPropertyNames.Contains(propertyName))
					{
						_mergedPropertyNames.Add(propertyName);
					}
				}
			}

			_mergedPropertyNames.Sort();

			return true;
		}

		public DataTable SettingsData
		{
			get
			{
				DataTable dt = new DataTable("Settings");
				dt.Columns.Add(new DataColumn("Property", typeof(string)));
				foreach (DeploymentEnvironment environment in _environmentList)
				{
					dt.Columns.Add(new DataColumn(environment.EnvironmentName, typeof(string)));
				}

				foreach (string property in _mergedPropertyNames)
				{
					DataRow row = dt.Rows.Add();
					row[0] = property;

					int ordinal = 1;
					foreach (DeploymentEnvironment environment in _environmentList)
					{
						if (environment.Properties.ContainsKey(property))
						{
							row[ordinal++] = environment.Properties[property];
						}
						else
						{
							row[ordinal++] = null;
						}
					}
				}

				return dt;
			}
		}


		public bool Save(DataTable dt)
		{

			// Build a list of properties to remove
			List<string> propertiesToRemove = new List<string>(_mergedPropertyNames);
			foreach (DataRow row in dt.Rows)
			{
				string propertyName = (string)row[0];
				propertiesToRemove.Remove(propertyName);
			}


			int environmentIndex = 0;
			foreach (DeploymentEnvironment environment in _environmentList)
			{
				if (!environment.EnvironmentName.Contains("_XREF_"))
				{
					foreach (DataRow row in dt.Rows)
					{
						string propertyName = (string)row[0];
						object val = row[environmentIndex + 1];
						string propertyValue = null;
						if (DBNull.Value != val)
						{
							propertyValue = (string)row[environmentIndex + 1];
						}

						if (environment.Properties.ContainsKey(propertyName))
						{
							environment.Properties[propertyName] = propertyValue;
						}
						else
						{
							environment.Properties.Add(propertyName, propertyValue);
						}
					}

					foreach (string propertyToRemove in propertiesToRemove)
					{
						environment.Properties.Remove(propertyToRemove);
					}

					environment.Save();
				}

				environmentIndex++;
			}
			return true;
		}
	}
}
